<template>
  <div class="login">
    <div class="login_box1">
      <div class="login_img">
        <img src="../assets/logo.png">
      </div>
      <el-form :model="ruleForm"  :rules="rules"  ref="reference" class="login_demo">
  <el-form-item  prop="username">
    <el-input prefix-icon="el-icon-s-custom" v-model="ruleForm.username"></el-input>
  </el-form-item>
  <el-form-item  prop="password">
    <el-input prefix-icon="el-icon-lock" type="password" v-model="ruleForm.password"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary"  @click="loginhome">登录</el-button>
    <el-button type="info" @click="resetForm">重置</el-button>
  </el-form-item>
</el-form>
      
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: "admin",
        password: "123456"
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 1, max: 9, message: "长度在 1 到 9个字符", trigger: "blur" }
        ]
      }
    };
  },

  mounted() {},

  methods: {
    resetForm() {
      this.$refs.reference.resetFields();
      console.log(this);
    },
    loginhome() {
      this.$refs.reference.validate(async valid => {
        if (!valid) {
          return;
        }
      });

      this.$axios.post("login", this.ruleForm).then(res => {
        // console.log(res);

        if (res.data.meta.status == 200) {
          // console.log(res.data.data.token);
          sessionStorage.setItem("data", res.data.data.token);
          this.$router.push("/home");
          alert("登录成功");
        } else {
          alert("密码错误");
        }
      });
    }
  }
};
</script>

<style>
.login_box1 {
  width: 450px;
  height: 300px;
  border-radius: 5px;
  text-align: right;
  background-color: #fff;
  position: relative;
  position: absolute;
  margin: auto;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 0 20px;
  box-shadow: 0 0 10px #efefef;
}
.login_img {
  width: 130px;
  height: 130px;
  padding: 10px;
  border-radius: 50%;
  position: absolute;
  top: -25%;
  left: 35%;
  background-color: #fff;
  box-shadow: 0 0 10px #efefef;
}
.login_demo {
  margin-top: 120px;
}
img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #efefef;
}
.login {
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
}
</style>

